<?php
namespace Home\Controller\Goods;
use Home\Controller\Auto\AutoController;
class AddressController extends AutoController {
	public function index(){
		// echo 'aaa';
		$data = M('g_address')->where('uid='.session('home_id'))->select();
		$this->list = $data;
		$this->addr=\Home\Controller\Goods\OrderController::xiangguan(135);
		$this->display();
	}
	public function d_api(){
		$url = "http://apis.juhe.cn/geo/?key=42567de920a9b3fa684bfdc83aea0231&lat=".I('post.wei')."&lng=".I('post.jing')."&type=2";
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$res = curl_exec($ch);
		// var_dump($res);
		// $this->AjaxReturn($res);
		echo $res;
		// var_dump(I('post.'));
	}
	public function insert(){
		// var_dump(I('post.'));
		$data=I('post.');
		$data['uid']=session('home_id');
		$address = M('g_address');
		if(count($address->where('uid='.session('home_id'))->select())==4){
			echo '<script>alert("最多可以拥有四个地址, 如果有需要,可以修改其他的!");location.href="/home/goods/address/index"</script>';
			return ;
		}
		if($address->where('uid='.session('home_id'))->select()){
			$data['status']=0;
		}else{
			$data['status']=1;
		}
		$address->create($data);
		if($address->add()){
			echo '<script>location.href="/home/goods/address/index"</script>';
		}else{
			echo '<script>alert("地址添加失败");location.href="/home/goods/address/index"</script>';
		}
	}
	public function status(){
		// var_dump(I('post.id'));
		// M('g_address')->where('id='.I('post.id'))->save([''])
		$address = M('g_address');
		$address->where('uid='.session('home_id').' and status=1')->save(['status'=>'0']);
		if($address->where('id='.I('post.id'))->save(['status'=>'1'])){
			echo 'yes';
			return ;
		}else{
			echo 'no';
			return ;
		}
	}
	public function update(){
		$address = M('g_address');
		$address->create(I('post.'));
		if($address->save()){
			echo '<script>alert("修改成功");location.href="/home/goods/address/index"</script>';
			return ;
		}else{
			echo '<script>alert("修改失败");location.href="/home/goods/address/index"</script>';
			return ;
		}
	}
	public function del(){
		if(M('g_address')->where('id='.I('post.id'))->delete()){
			echo 'yes';
		}else{
			echo 'no';
		}
	}
}